package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.DeleteAnnotation;
import com.baizhi.annotation.InsertAnnotation;
import com.baizhi.dao.ArticleDao;
import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired(required = false)
    private ArticleDao articleDao;

    //展示全部数据并进行分页
    @Override
    @InsertAnnotation
    public Map selectAll(int page, int limit, String articleName) {
        int offset=(page-1)*limit;
        List<Article> list=articleDao.selectAll(offset,limit);
        int count=articleDao.selectCount(articleName);
        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        System.out.println(list);
        return map;
    }

    //根据ID展示一条数据
    @Override
    @InsertAnnotation
    public Article selectOne(int id) {
        return articleDao.selectOne(id);
    }

    //添加
    @Override
    @DeleteAnnotation
    public Map insert(Article article) {
        int count=articleDao.insert(article);
        Map map=new HashMap();
        if(count>0){
            map.put("isInsert",true);
        }else{
            map.put("isInsert",false);
        }
        return map;
    }

    //删除
    @Override
    @DeleteAnnotation
    public Map delete(int id) {
        int count=articleDao.delete(id);
        Map map=new HashMap();
        if(count>0){
            map.put("isDelete","删除成功");
        }else{
            map.put("isDelete","删除失败");
        }
        return map;
    }

    //更新
    @Override
    @DeleteAnnotation
    public Map update(Article article) {
        int count=articleDao.update(article);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","更新成功");
        }else{
            map.put("isUpdate","更新失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map updatea(Article article) {
        int count=articleDao.updatea(article);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","更新成功");
        }else{
            map.put("isUpdate","更新失败");
        }

        System.out.println("updatea"+article);
        return map;
    }

    @Override
    @InsertAnnotation
    public Map selectAlls() {
        List list = articleDao.selectAlls();
        Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams("文章信息表","article"),Article.class,list) ;
        try {
            workbook.write(new FileOutputStream("C://Users//小龙//Desktop//新建文件夹//article.xlsx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map=new HashMap();
        map.put("data",list);
        if(workbook!=null){
            map.put("isxiazai","导出成功");
        }else{
            map.put("isxiazai","导出失败");
        }
        return map;
    }
}
